#region FileHeader
//////////////////////////////////////////////////////////////////////////////////
// Author:                          Hector Llanos
// Date:                            12.06.2011
// FileName:                        PathManager.cs
// For:                             Sound Defense
// Reason:                          contains all the paths in the game
//////////////////////////////////////////////////////////////////////////////////
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LoopType = iTween.LoopType;

[ExecuteInEditMode]
public class PathManager
{
    #region Instance
    private static PathManager m_instance;
    public static PathManager Instance
    {
        get
        {
            if (m_instance == null)
            {
                m_instance = new PathManager();
            }

            return m_instance;
        }
    }
    private PathManager() { }
    #endregion

    #region FIELDS
    private Dictionary<string, Vector3[]> m_paths;
    #endregion

    #region PUBLIC_INTERFACE

    /// <summary>
    /// Inits the PathManager
    /// </summary>
    /// <return> void </return>
    public void Initialize()
    {
        //allocate dictionary
        m_paths = new Dictionary<string, Vector3[]>();
    }

    public void CollectPathsFromHierarchy()
    {
        //find all the objects with the Path tag
        GameObject[] paths = GameObject.FindGameObjectsWithTag("Path");
        foreach (GameObject go in paths)
        {
            //add each path to the manager
            Path path = go.GetComponent<Path>();
            Debug.Log(path.name);
            m_paths.Add(path.name, path.GetNodes().ToArray());
        }
    }
    /// <summary>
    /// gets any path already loaded
    /// </summary>
    /// <return> void or null </return>
    public Vector3[] GetPath(string pathName)
    {
        if (m_paths.ContainsKey(pathName))
        {
            return m_paths[pathName];
        }
        return null;
    }
    /// <summary>
    /// adds a path the the dictionary
    /// </summary>
    /// <return> void or null </return>
    public void AddPath(string name, Vector3[] path)
    {
        if (path == null || m_paths == null)
            return;

        //if its not in here, add it
        if (!m_paths.ContainsKey(name))
        {
            m_paths.Add(name, path);
        }
    }


    public void AddPath(string name, float[] pathArr)
    {
        //if the name is already in the dictionary.. problem
        if (m_paths.ContainsKey(name))
        {
            return;
        }

        //get the length of the array
        int length = pathArr.Length;

        //create a container for the path
        List<Vector3> path = new List<Vector3>();

        for (int i = 0; i < length; i += 3)
        {
            //get the positions
            Vector3 tempVec = new Vector3(pathArr[i], pathArr[i + 1], pathArr[i + 2]);

            //add it to the temp list
            path.Add(tempVec);
        }

        //add it to the dictionary
        m_paths.Add(name, path.ToArray());
    }
    public void RemovePath(string m_name)
    {
        if (m_paths.ContainsKey(m_name))
            m_paths.Remove(m_name);
    }
    #endregion

}